/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.stripes.action;

import able.stripes.JSP;
import com.google.inject.Inject;
import exatrez.dao.VesselGroupDAO;
import exatrez.manager.VesselManager;
import exatrez.model.InfoAudit;
import exatrez.model.User;
import exatrez.model.VesselGroup;
import exatrez.stripes.BaseActionBean;
import exatrez.stripes.config.AuthRequired;
import java.util.Date;
import java.util.List;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author wan.fairul
 */
@UrlBinding("/adminVesselGroup")
@AuthRequired
public class VesselGroupAction extends BaseActionBean {

    private static Logger LOG = LoggerFactory.getLogger(VesselGroupAction.class);
    private VesselGroup vesselGroup;
    private List<VesselGroup> listofVesselGroup;
    @Inject
    private VesselManager vesselManager;
    @Inject
    private VesselGroupDAO vesselGroupDAO;

    @DefaultHandler
    public Resolution add_vesselGroup() {
        vesselGroup = new VesselGroup();
        listofVesselGroup = vesselManager.findAllVesselGroup();
        return new JSP("admin/vessel_group.jsp");
    }

    public Resolution saveVesselGroup() {
        VesselGroup vg = vesselManager.findVesselUser(String.valueOf(vesselGroup.getVessel().getVesselId()), vesselGroup.getUser().getUserId());
        User user = getContext().getCurrentUser();
        if (vg == null) {
            InfoAudit ia = new InfoAudit();
            ia.setCreateDate(new Date());
            ia.setCreateId(user);
            vesselGroup.setInfoAudit(ia);
            vesselGroup.setActive('Y');
            vesselManager.saveOrUpdateVesselGroup(vesselGroup);
            addSimpleMessage("Success");
            LOG.debug("finish");
        } else {
            addSimpleError("Duplicate!");
        }
        return add_vesselGroup();
    }

    public Resolution deleteVesselGroup() {
        String vesselGroupId = getContext().getRequest().getParameter("vesselGroupId");
        if (vesselGroupId != null) {
            vesselGroup = vesselGroupDAO.findById(Integer.parseInt(vesselGroupId));
            if (vesselGroup != null) {
                vesselManager.deleteVesselGroup(vesselGroup);
                addSimpleMessage("Success Delete");
            }
        }
        return new RedirectResolution(VesselGroupAction.class, "add_vesselGroup");
    }

    public Resolution editVesselGroup() {
        String vesselGroupId = getContext().getRequest().getParameter("vesselGroupId");
        if (vesselGroupId != null) {
            vesselGroup = vesselGroupDAO.findById(Integer.parseInt(vesselGroupId));
        }
        return new JSP("admin/edit_vesselGroup.jsp").addParameter("popup", true);
    }

    public Resolution saveEditVesselGroup() {
        String vesselGroupId = getContext().getRequest().getParameter("vesselGroup.vesselGroupId");
        if (vesselGroupId != null) {
            User user = getContext().getCurrentUser();
            VesselGroup vg = vesselGroupDAO.findById(Integer.parseInt(vesselGroupId));
            InfoAudit ia = vg.getInfoAudit();
            vg.setVessel(vesselGroup.getVessel());
            vg.setUser(vesselGroup.getUser());
            vg.setActive(vesselGroup.getActive());
            ia.setUpdateDate(new Date());
            ia.setUpdateId(user);
            vg.setInfoAudit(ia);
            vesselManager.saveOrUpdateVesselGroup(vg);
            addSimpleMessage("Success Update");
        }
        return new RedirectResolution(VesselGroupAction.class, "add_vesselGroup");
    }

    public VesselGroupDAO getVesselGroupDAO() {
        return vesselGroupDAO;
    }

    public void setVesselGroupDAO(VesselGroupDAO vesselGroupDAO) {
        this.vesselGroupDAO = vesselGroupDAO;
    }

    public VesselGroup getVesselGroup() {
        return vesselGroup;
    }

    public void setVesselGroup(VesselGroup vesselGroup) {
        this.vesselGroup = vesselGroup;
    }

    public List<VesselGroup> getListofVesselGroup() {
        return listofVesselGroup;
    }

    public void setListofVesselGroup(List<VesselGroup> listofVesselGroup) {
        this.listofVesselGroup = listofVesselGroup;
    }

    public VesselManager getVesselManager() {
        return vesselManager;
    }

    public void setVesselManager(VesselManager vesselManager) {
        this.vesselManager = vesselManager;
    }
}
